package Client.GUI;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;


// import javafx.scene.layout.Border;
import java.net.*;

import javax.sound.sampled.Port;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JViewport;

import Client.VerbindungsAufbauClient;
//import Client.Model.Spiellogik;
import Commons.Spieler;

/**
 * 
 * @author Alex
 *
 */

public class SpielbeitretenGUI extends JFrame implements ActionListener {
	
	// Panels erstellen
	JPanel mainpanel = new JPanel();
	
	//JPanel panel1 = new JPanel();
	JPanel panel2 = new JPanel();
	JPanel panel21 = new JPanel();
	JPanel panel22 = new JPanel();
	JPanel panel23 = new JPanel();
	JPanel panel3 = new JPanel();
	JPanel panel31 = new JPanel();
	
	
	//Label erstellen
	JLabel IPLabel = new JLabel("IP Adresse eingeben ");
	JLabel PortLabel = new JLabel("      Port eingeben     ");
	JLabel NameLabel = new JLabel(" Username eingeben "); 
	
	//Text
	static JTextField IPtextfield = new JTextField("127.0.0.1", 16);
	static JTextField Porttextfield2 = new JTextField("4444", 16);
	public static JTextField Nametextfield = new JTextField("Spieler 2", 16);
	
	
	//Buttons
	JButton confirm = new JButton("Verbinden");
	JButton cancel = new JButton("Abbrechen");
	
	
	public SpielbeitretenGUI() throws UnknownHostException{
		//konstruktor
			this.setTitle("Haggis");
//			setPreferredSize(new Dimension(MainClass.getWidth(),MainClass.getHeight()));
			setPreferredSize(new Dimension(850, 450));
			setBounds(BildschirmGroesse.getWidth()/2-425, BildschirmGroesse.getHeight()/2-225, 550, 200); // Platzierung und gr�sse des Main Framesetzen
			setResizable(true);
			setVisible(true);
			this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
			
			
			//layout manager
			
			mainpanel.setLayout(new BorderLayout());
			getContentPane().add(mainpanel);
			
			panel2.setLayout(new BorderLayout());
			
			
			//fill the mainpanel with 3 panels-- panel1, panel2 and panel3
			
			mainpanel.add(panel2, BorderLayout.CENTER);
			mainpanel.add(panel3, BorderLayout.SOUTH);
			
			 panel2.add(panel21, BorderLayout.NORTH);
		     panel2.add(panel22,BorderLayout.CENTER);
		     panel2.add(panel23,BorderLayout.SOUTH);
			
	        
	        //Schriftart anpassen
			IPLabel.setFont(IPLabel.getFont().deriveFont(20f));
			PortLabel.setFont(PortLabel.getFont().deriveFont(20f));
			NameLabel.setFont(NameLabel.getFont().deriveFont(20f));
			confirm.setFont(confirm.getFont().deriveFont(16f));
			cancel.setFont(cancel.getFont().deriveFont(16f));
	        
	        
	        
	        //fill panel2 with components
	        panel21.add(IPLabel);
	        panel21.add(IPtextfield);
	        panel21.setLayout(new GridBagLayout());
	        panel21.setBorder(BorderFactory.createEmptyBorder(35, 10, 10, 5));
	        
	        panel22.add(PortLabel);
	        panel22.add(Porttextfield2);
	        panel22.setLayout(new GridBagLayout());
	        panel22.setBorder(BorderFactory.createEmptyBorder(5, 10, 10, 5));
	        
	        panel23.add(NameLabel);
	        panel23.add(Nametextfield);
	        

	        
	        
	        //set panel2 in the middle of the screen with gridlayout	
//			panel2.setLayout(new GridBagLayout());
//	        panel2.setBorder(BorderFactory.createEmptyBorder(5, 10, 10, 5));
	       
			
	        
	        panel31.add(confirm);
	        panel31.add(cancel);
	        panel3.add(panel31);
	        panel3.setLayout(new GridBagLayout());
	        panel3.setBorder(BorderFactory.createEmptyBorder(20, 10, 15, 5));
	        
	        InetAddress testing = InetAddress.getLocalHost();
			String ipadresse = testing.getHostAddress();
	        
			
	        pack();
			setVisible(true);
			
			//Logik - Button gedr�ckt
			
			cancel.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					try {
						HomeGUI canceled = new HomeGUI();
				// Fehler abfangen
					} catch (Exception ex) { // inkorrekte Eingabe
						JOptionPane.showMessageDialog(null,
								"Kann nicht abgebrochen werden!", "Fehler",
								JOptionPane.INFORMATION_MESSAGE); // Dialogbox
					}
				}
			});	
			
			
			
			confirm.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					try {
						//client will be started when the button is clicked
						new VerbindungsAufbauClient().connect(getPorttextfield2(), getNametextfield());
						
						
						/**
						 * line 165 has to be activated if you want to play on 2 different notebooks and line 159 must be deactivated
						 */
						//new VerbindungsAufbauClient().connect(getPorttextfield2(), getNametextfield(), getIPtextfield());
						confirm.setEnabled(false);
						if (confirm.isEnabled() == false) {
							SpielbeitretenGUI.this.dispose();
						
							
						}
						
						

						}
						
						
				// Fehler abfangen
					 catch (Exception ex) { // inkorrekte Eingabe
						JOptionPane.showMessageDialog(null,
								"Kann nicht abgebrochen werden!", "Fehler",
								JOptionPane.INFORMATION_MESSAGE); // Dialogbox
					}
				}
			});	
			
			
		
	}
	
	
	
	public static int getPorttextfield2() {
			String s = Porttextfield2.getText();
			int portnumber = (Integer.parseInt(s));
			return portnumber;
		}



	
	
//	public static void main(String[] args) {
//		SpielbeitretenGUI beitreten12 = new SpielbeitretenGUI();
//		}


	public static String getIPtextfield() {
		String s = IPtextfield.getText();
		return s;
	}



	public static String getNametextfield() {
		String s = Nametextfield.getText();
		return s;
	}



	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		
	}
	
	

}
